
public class PointLight extends Light {

	private Vector pos = null;
	private double[] attenuation = null;
	
	public PointLight(){
		this.pos = new Vector();
		this.attenuation = new double[3];
		this.attenuation[0]=1;
		this.attenuation[1]=0;
		this.attenuation[2]=0;
		this.setColor(new RGB(1.0,1.0,1.0));
	}
	
	public PointLight(Vector pos, double[] attenuation){
		this();
		this.pos = pos;
		if (attenuation != null) this.attenuation = attenuation;
	}
	
	public PointLight(Vector pos, double[] attenuation, RGB color){
		this(pos, attenuation);
		if (color != null) this.setColor(color);
	}
	
	public void setPos(Vector pos) {
		this.pos = pos;
	}

	public Vector getPos(){
		return this.pos;
	}
	
	public void setAttenuation(double[] attenuation){
		this.attenuation = attenuation;
	}
	
	public void setAttenuation(double kc, double kl, double kq) {
		this.attenuation[0] = kc;
		this.attenuation[1] = kl;
		this.attenuation[2] = kq;
	}
	
	public double[] getAttenuation(){
		return this.attenuation;
	}
	
	@Override
	public Vector getDirection(Vector target) {
		return Vector.sub_n(target, this.pos);
	}
	
	@Override
	public boolean isBlockedByPrimitive(Vector target, Vector viewDirection, Primitive p) {
		double totalDist_Q = Vector.getDistance_Q(this.pos,target);
		Vector intersectionPoint = p.intersectionPoint(target, this.pos);
		if( intersectionPoint == null) return false;
		if( !target.isEqual_n(intersectionPoint) && (Vector.getDistance_Q(target,intersectionPoint) > totalDist_Q) ){
			return false;
		}
		if(Vector.isHiding(target, intersectionPoint, viewDirection, getDirection(target), p)){
			return true;
		}
		return false;	
	}

	@Override
	public RGB getLightningAt(Vector target) {
		double dist_Q = Vector.getDistance_Q(this.pos, target);
		double dist = Math.sqrt(dist_Q);
		double[] att = this.attenuation;
		double reductionFactor = 1 / (att[0] + att[1]*dist + att[2]*dist_Q) ;
		return this.getColor().mul(reductionFactor);
	}
	
	public static PointLight getDBGplight(){
		//return new PointLight(new Vector(2,0,-4),new Vector(1.0,0,0), null);
		double[] attenuation = {1.0, 0.0, 0.0};
		return new PointLight(new Vector(2,0,-4), attenuation);
	}
	
	public static PointLight getDBGplight2(){
		double[] attenuation = {1.0, 0.0, 0.0};
		return new PointLight(new Vector(2,0,4),attenuation);
	}
}
